package com.sunq.studio.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.sunq.studio.sys.dao.TbSysBookOrderDao;
import com.sunq.studio.sys.dao.TbUserDao;
import com.sunq.studio.sys.domain.TbSysBookOrder;
import com.sunq.studio.sys.domain.TbUser;
import com.sunq.studio.sys.service.PayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by sunq on 2019/7/30.
 */
@Service
public class PayServiceImpl  implements PayService{


    static Logger logger= LoggerFactory.getLogger(PayServiceImpl.class);


    @Autowired
    private  TbUserDao tbUserDao;
    @Autowired
    private TbSysBookOrderDao tbSysBookOrderDao;

    @Override
    @Transactional
    public TbSysBookOrder Save(TbSysBookOrder tbSysBookOrder) {


       int i= tbSysBookOrderDao.insertSelective(tbSysBookOrder);

       if (i==1){
           logger.info("订单插入成功");
       }else{
           logger.info("订单插入失败");
       }

        return null;
    }

    @Override
    @Transactional
    public int notifySuccess(Map map) {
        String tradeNo=(String)map.get("out_trade_no");
        TbSysBookOrder tbSysBookOrder=tbSysBookOrderDao.selectOrderByOrderId(tradeNo);
        if(tbSysBookOrder==null||tbSysBookOrder.getId()==null){
            logger.info("无法找到订单");
            return 0;
        }

        TbSysBookOrder updateOrder=new TbSysBookOrder();
        if(tbSysBookOrder.getOrderStatus()==1){
            logger.info("支付回调dao 开始处理");
            updateOrder.setId(tbSysBookOrder.getId());
            updateOrder.setPayTime(new Date());
            updateOrder.setTransactionId((String)map.get("transaction_id"));
            updateOrder.setOrderStatus(2);
            tbSysBookOrderDao.updateByPrimaryKeySelective(updateOrder);

            TbUser user=tbUserDao.selectByPrimaryKey(tbSysBookOrder.getUserId());
            if (  (user!=null) &&(user.getId()!=null) ){
                logger.info("支付回调用户金额 处理");
                TbUser updateUser= new TbUser();
                updateUser.setId(user.getId());
                updateUser.setMoney(user.getMoney().add(tbSysBookOrder.getPayMoney()));
                updateUser.setVipLevel(1);
                tbUserDao.updateByPrimaryKeySelective(updateUser);
            }

        }else{
            logger.info("支付回调已处理");
        }
        return 1;
    }
    @Override
    @Transactional
    public int notifyFail(Map map) {
        String tradeNo=(String)map.get("out_trade_no");
        TbSysBookOrder tbSysBookOrder=tbSysBookOrderDao.selectOrderByOrderId(tradeNo);
        if(tbSysBookOrder==null||tbSysBookOrder.getId()==null){
            logger.info("无法找到订单");
            return 0;
        }
        TbSysBookOrder updateOrder=new TbSysBookOrder();
        if(tbSysBookOrder.getOrderStatus()==1){
            logger.info("支付回调dao 开始处理");
            updateOrder.setId(tbSysBookOrder.getId());
            updateOrder.setPayTime(new Date());
            updateOrder.setTransactionId((String)map.get("transaction_id"));
            updateOrder.setOrderStatus(3);
            tbSysBookOrderDao.updateByPrimaryKeySelective(updateOrder);



        }else{
            logger.info("支付回调已处理");
        }
        return 1;
    }
    @Override
    @Transactional
    public int notifyFail(TbSysBookOrder bookOrder) {
        String tradeNo=(String)bookOrder.getOutTradeId();
        TbSysBookOrder tbSysBookOrder=tbSysBookOrderDao.selectOrderByOrderId(tradeNo);
        if(tbSysBookOrder==null||tbSysBookOrder.getId()==null){
            logger.info("无法找到订单");
            return 0;
        }
        TbSysBookOrder updateOrder=new TbSysBookOrder();
        if(tbSysBookOrder.getOrderStatus()==1){
            logger.info("支付取消 开始处理");
            updateOrder.setId(tbSysBookOrder.getId());
            updateOrder.setOrderStatus(4);
            tbSysBookOrderDao.updateByPrimaryKeySelective(updateOrder);

        }else{
            logger.info("支付取消已处理");
        }
        return 1;
    }

    @Override
    public TbSysBookOrder selectOrderByOrderId(String orderId) {
        return null;
    }

    @Override
    public List<TbSysBookOrder> getOrderByUserId(Integer userId,int page) {

        PageHelper.startPage(page,20);
        List<TbSysBookOrder>  res=  tbSysBookOrderDao.getOrderByUserId(userId);

        SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" );
        res.stream().forEach(t->t.setOrderTimeStr(sdf1.format(t.getOrderTime() )) );

        return res;
    }
}
